package cn.cnsy123.blog.application.encoder.strategy;

import cn.cnsy123.blog.application.encoder.PasswordEncoder;
import org.apache.commons.codec.binary.Hex;
import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;

/**
 * @author yangpengfei
 * @description MD5加密方式
 * @date 2019-10-15 18-40-29
 */
@Component
public class Md5PasswordStrategy  implements PasswordEncoder {

    @Override
    public String encode(String oldPass, String passSalt) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            String source = new StringBuffer().append(passSalt).append(oldPass).toString();
            md5.update(source.getBytes("UTF-8"));
            byte [] result = md5.digest();

            return Hex.encodeHexString(result);
        }catch (Exception e){

        }
        return null;
    }


    @Override
    public boolean equals(String encryptPass, String inputPass,String passSalt) {
        return encode(inputPass,passSalt).equals(encryptPass);
    }
}
